package hedron.dialog;

import hedron.Activator;
import hedron.Application;
import hedron.db.model.Stock;
import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class PickStockDialog extends AbstractDialog {

	final String title;
	final String message;

	Text filter;
	ArrayList<Stock> stocks;
	Table stocksTable;

	public Stock selectedStock;

	public PickStockDialog(Shell parentShell, String title, String message) {
		super(parentShell);
		this.title = title;
		this.message = message;
	}

	void refresh() {
		stocksTable.setRedraw(false);
		stocksTable.removeAll();
		stocks = Application.STOCKS.getStocks(filter.getText(), "GROUP_NAME", "ASC");
		for (Stock stock : stocks) {
			TableItem item = new TableItem(stocksTable, SWT.NONE);
			item.setData(stock);
			item.setText(0, stock.GROUP_NAME);
			item.setText(1, stock.STOCK_NAME);
		}
		if (stocks.size() > 0) {
			stocksTable.setSelection(0);
		}

		stocksTable.getColumn(0).pack();
		stocksTable.getColumn(1).pack();
		stocksTable.setRedraw(true);
	}

	@Override
	protected void okPressed() {
		int selection = stocksTable.getSelectionIndex();
		if (selection >= 0) {
			selectedStock = (Stock) stocksTable.getItem(selection).getData();
			setReturnCode(OK);
			close();
		}
	}

	@Override
	public void create() {
		setDefaultImage(Activator.getImage("icons/stock.png"));
		super.create();
		setTitle(title);
		setMessage(message, 0);
	}

	@Override
	void createContent(Composite parent) {
		{
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			gd.horizontalIndent = 0;
			gd.verticalIndent = 0;
			gd.widthHint = 400;
			gd.heightHint = 200;
			parent.setLayoutData(gd);
		}
		Composite grid = new Composite(parent, 0);

		{// set parent layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 1;
			grid.setLayout(layout);

			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			grid.setLayoutData(gd);
		}
		{// filter
			Text text = new Text(grid, SWT.BORDER);
			GridData gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
			text.setLayoutData(gdata);
			text.addKeyListener(new KeyListener() {
				@Override
				public void keyReleased(KeyEvent e) {
					refresh();
				}

				@Override
				public void keyPressed(KeyEvent e) {}
			});
			filter = text;
		}
		{
			Table table = new Table(grid, SWT.FULL_SELECTION | SWT.BORDER);
			table.setLayoutData(new GridData(GridData.FILL_BOTH));

			{
				TableColumn column = new TableColumn(table, SWT.NONE);
				column.setText("Група");
			}
			{
				TableColumn column = new TableColumn(table, SWT.NONE);
				column.setText("Стока");
			}
			table.setHeaderVisible(true);
			table.setLinesVisible(true);

			table.addListener(SWT.MouseDoubleClick, new Listener() {
				@Override
				public void handleEvent(Event event) {
					okPressed();
				}
			});
			stocksTable = table;
		}
		refresh();
	}

	@Override
	void createFooter(Composite parent) {
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));

		{// set grid layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 3;
			grid.setLayout(layout);
		}
		{// cell 1 - empty text
			Label label = new Label(grid, 0);
			GridData gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
			label.setLayoutData(gdata);
			label.setFocus();
		}
		{// cancel
			Button button = new Button(grid, SWT.PUSH);
			button.setText("Cancel");
			GridData gdata = new GridData(GridData.BEGINNING, GridData.END, false, false);
			gdata.widthHint = 80;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					setReturnCode(CANCEL);
					close();
				}
			});
		}
		{// ok
			Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			GridData gdata = new GridData(GridData.BEGINNING, GridData.END, false, false);
			gdata.widthHint = 80;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					okPressed();
				}
			});

			// set button as default
			Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
	}
}
